/**
 *
 */
package com.storm.lib.util;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

import javax.xml.parsers.ParserConfigurationException;


import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import org.w3c.dom.Element;
import org.xml.sax.SAXException;

import com.storm.lib.type.SystemType;


/**
 * 类功能:
 * 
 * @author johnny
 * @version 2011-2-21
 */
public class XMLUtil {
	private static javax.xml.parsers.DocumentBuilderFactory documentBuilderFactory = null;

	/**
	 * 方法功能:doc转w3c
	 * @author future 2011-11-8
	 * @param doc
	 * @return
	 */
	public static org.w3c.dom.Document parse(Document doc) {
		java.io.StringReader reader = new java.io.StringReader(doc.asXML());
		org.xml.sax.InputSource source = new org.xml.sax.InputSource(reader);
		if (documentBuilderFactory == null) {
			documentBuilderFactory = javax.xml.parsers.DocumentBuilderFactory.newInstance();
		}
		try {
			javax.xml.parsers.DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
			return documentBuilder.parse(source);
		} catch (ParserConfigurationException e) {
			ExceptionUtil.processException(e);
		} catch (SAXException e) {
			ExceptionUtil.processException(e);
		} catch (IOException e) {
			ExceptionUtil.processException(e);
		}
		return null;
	}

	public static Document getDom4JDocument(String serverXML) {
		Document doc = null;
		try {
			doc = DocumentHelper.parseText(serverXML);
		} catch (DocumentException e) {
			ExceptionUtil.processException(e);
		}
		return doc;
	}

	/**
	 * 
	 * 方法功能:获得Dom4j文档 更新时间:2011-6-15, 作者:johnny
	 * 
	 * @param file
	 * @return
	 */
	public static Document getDom4JDocument(File file) {
		SAXReader reader = new SAXReader();
		Document document = null;
		try {
			document = reader.read(file);
		} catch (DocumentException e) {
			ExceptionUtil.processException(e);
		}
		return document;
	}
	
	public static org.w3c.dom.Document createDoc(org.dom4j.Element element){
		Document doc = DocumentHelper.createDocument();
        doc.setXMLEncoding("utf-8");
        doc.addProcessingInstruction("xml-stylesheet", "type='text/xsl' href='students.xsl'");
        org.dom4j.Element root = DocumentHelper.createElement("battle_round");
        doc.setRootElement(root);
//        org.dom4j.Element round = DocumentHelper.createElement("round");
//        root.add(round);
        
        //org.dom4j.Element _root = document.getRootElement();
//        List<org.dom4j.Element> _list = element.elements("round");
//        org.dom4j.Element _round = null;
//        for(org.dom4j.Element e:_list){
//        	int count = Integer.parseInt(e.attribute("round_count").getData().toString());
//        	if(count==roundCount){
//        		_round = e;
//        	}
//        }
        List<org.dom4j.Element> list = element.elements();
        for(org.dom4j.Element e:list){
        	String name = e.getName();
        	org.dom4j.Element tmp = DocumentHelper.createElement(name);
        	List<Attribute> listAtb = e.attributes();
        	for(Attribute a:listAtb){
        		tmp.addAttribute(a.getName(), a.getData().toString());
        	}
        	root.add(tmp);
        }
        
		try {
			return parse(doc);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static void main(String[] args) {
		Document doc = DocumentHelper.createDocument();
        doc.setXMLEncoding("utf-8");
        doc.addProcessingInstruction("xml-stylesheet", "type='text/xsl' href='students.xsl'");
        org.dom4j.Element root = DocumentHelper.createElement("battle_round");
        doc.setRootElement(root);
//        org.dom4j.Element round = DocumentHelper.createElement("root_round");
        
        
        org.dom4j.Element round1 = DocumentHelper.createElement("round");
        org.dom4j.Element r1 = DocumentHelper.createElement("round1");
        r1.addAttribute("name1","tt1");
        r1.addAttribute("name2","tt2");
        round1.add(r1);
        root.add(round1);
        
        org.dom4j.Element round2 = DocumentHelper.createElement("round");
        org.dom4j.Element r2 = DocumentHelper.createElement("round2");
        r2.addAttribute("name1","tt1");
        r2.addAttribute("name2","tt2");
        round2.add(r2);
        root.add(round2);
        
        OutputFormat outFmt = new OutputFormat(" ", true);
		outFmt.setEncoding("utf-8");
		try {
			BufferedOutputStream Buff = new BufferedOutputStream(new FileOutputStream("f:\\12.xml"));
			XMLWriter xmlWriter = new XMLWriter(Buff, outFmt);
			xmlWriter.write(doc);
			xmlWriter.close();
		} catch (Exception e) {
			ExceptionUtil.processException(e);
		}
	}

}
